package com.kate.mall.product.service.impl;

import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kate.mall.common.utils.PageUtils;
import com.kate.mall.common.utils.Query;

import com.kate.mall.product.dao.GoodsDao;
import com.kate.mall.product.entity.GoodsEntity;
import com.kate.mall.product.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                generatorQueryWrapper(params)
        );

        return new PageUtils(page);
    }

    /**
     * 构建查询条件的方法
     * @param params
     * @return
     */
    private QueryWrapper generatorQueryWrapper(Map<String, Object> params){
        QueryWrapper<GoodsEntity> goodsEntityQueryWrapper = new QueryWrapper<>();
        //新品
        if(params.containsKey("isNew")){
            goodsEntityQueryWrapper.lambda().eq(GoodsEntity::getIsNew,Integer.parseInt((String)params.get("isNew")));
        }
        //热卖
        if(params.containsKey("isHot")){
            goodsEntityQueryWrapper.lambda().eq(GoodsEntity::getIsHot,Integer.parseInt((String)params.get("isHot")));
        }
        return goodsEntityQueryWrapper;
    }

}